package org.dromara.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.comm.domain.AsActivity;
import org.dromara.comm.mapper.AsActivityMapper;
import org.dromara.comm_lhz.entity.AsActivityEntity;
import org.dromara.comm_lhz.entity.AsSignupActivity;
import org.dromara.comm_lhz.mapper.AsActivityHMapper;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.community.entity.AsRegistrationInformation;
import org.dromara.community.mapper.AsRegistrationInformationsMapper;
import org.dromara.community.mapper.AsSignupActivityMapper;
import org.dromara.community.service.AsRegistrationInformationsService;
import org.dromara.system.domain.SysUser;
import org.dromara.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AsRegistrationInformationsServiceimpl extends ServiceImpl<AsRegistrationInformationsMapper, AsRegistrationInformation> implements AsRegistrationInformationsService {

    @Autowired
    private AsRegistrationInformationsMapper asRegistrationInformationMapper;

    @Autowired
    private AsActivityMapper asActivityMapper;

    @Autowired
    private AsActivityHMapper AsActivityHMapper;

    @Autowired
    private AsSignupActivityMapper asSignupActivityMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
/**
 * 根据用户ID查询用户的所有信息
 * 此方法主要查询用户的头像和注册信息，是为了完整地获取用户的相关数据
 *
 * @param userId 用户的ID，用于查询用户信息
 * @return 返回一个包含用户头像和注册信息的列表
 */
    public Page<AsRegistrationInformation> selectAllid(AsRegistrationInformation asRegistrationInformation,Pageable pageable) {
//        // 创建一个映射结果集，用于存放查询到的用户信息
//        Map<String, Object> resultMap = new HashMap<>();
//
//        // 获取所有 AsRegistrationInformation 数据
//        List<AsRegistrationInformation> informationList = asRegistrationInformationMapper.selectList(null);
//
//        // 创建一个用于存储最终结果的对象
//        Map<String, Object> asActivityMap = new HashMap<>();
//
//        // 遍历 AsRegistrationInformation 列表
//        for (AsRegistrationInformation information : informationList) {
//            // 查找与 AsRegistrationInformation 对应的 AsSignupActivity
//            LambdaQueryWrapper<AsSignupActivity> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(AsSignupActivity::getSignupId, information.getId());
//            List<AsSignupActivity> activityList = asSignupActivityMapper.selectList(wrapper);
//
//            // 处理每个 AsSignupActivity
//            for (AsSignupActivity activity : activityList) {
//                // 查找与 AsSignupActivity 对应的 AsActivityEntity
//                LambdaQueryWrapper<AsActivityEntity> queryWrapper = new LambdaQueryWrapper<>();
//                queryWrapper.eq(AsActivityEntity::getId, activity.getActivityId());
//                List<AsActivityEntity> activityEntities = AsActivityHMapper.selectList(queryWrapper);
//
//                // 处理每个 AsActivityEntity
//                for (AsActivityEntity activityEntity : activityEntities) {
//                    // 将 AsActivityEntity 的信息存储在 asActivityMap 中
//                    asActivityMap.put("id", activityEntity.getId());
//                    asActivityMap.put("userId", information.getUserId());
//                    asActivityMap.put("activityId", activityEntity.getId());
//                    asActivityMap.put("name", information.getName());
//                    asActivityMap.put("phone", information.getPhone());
//                    asActivityMap.put("sex", information.getSex());
//                    asActivityMap.put("age", information.getAge());
//                    asActivityMap.put("createBy", activityEntity.getCreateBy());
//                    asActivityMap.put("createTime", activityEntity.getCreateTime());
//                    asActivityMap.put("updateBy", activityEntity.getUpdateBy());
//                    asActivityMap.put("updateTime", activityEntity.getUpdateTime());
//                    asActivityMap.put("isDelete", activityEntity.getIsDelete());
//
//                    // 创建 aaa 列表并填充
//                    List<Map<String, Object>> aaaList = new ArrayList<>();
//
//                    // 将每个 AsSignupActivity 的信息添加到 aaaList 中
//                    Map<String, Object> aaaMap = new HashMap<>();
//                    aaaMap.put("id", activity.getId());
//                    aaaMap.put("signupId", activity.getSignupId());
//                    aaaMap.put("activityId", activity.getActivityId());
//
//                    // 创建 bbb 列表并填充
//                    List<Map<String, Object>> bbbList = new ArrayList<>();
//
//                    // 查找与 AsSignupActivity 对应的 AsActivityEntity 数据
//                    LambdaQueryWrapper<AsActivityEntity> bbbWrapper = new LambdaQueryWrapper<>();
//                    bbbWrapper.eq(AsActivityEntity::getId, activity.getActivityId());
//                    List<AsActivityEntity> bbbEntities = AsActivityHMapper.selectList(bbbWrapper);
//
//                    // 将每个 AsActivityEntity 的信息添加到 bbbList 中
//                    for (AsActivityEntity bbbEntity : bbbEntities) {
//                        Map<String, Object> bbbMap = new HashMap<>();
//                        bbbMap.put("id", bbbEntity.getId());
//                        bbbMap.put("activityName", bbbEntity.getActivityName());
//                        bbbMap.put("createTime", bbbEntity.getCreateTime());
//                        bbbMap.put("updateTime", bbbEntity.getUpdateTime());
//                        bbbList.add(bbbMap);
//                    }
//
//                    // 将 bbbList 添加到 aaaMap 中
//                    aaaMap.put("bbb", bbbList);
//                    aaaList.add(aaaMap);
//
//                    // 将 aaaList 添加到 asActivityMap 中
//                    asActivityMap.put("aaa", aaaList);
//                }
//            }
//        }
//
//        // 将 asActivityMap 添加到 resultMap 中
//        resultMap.put("asActivity", asActivityMap);
//
//        return resultMap;
        Page<AsRegistrationInformation> page = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
        LambdaQueryWrapper<AsRegistrationInformation> queryWrapper = new LambdaQueryWrapper<>();
        if(asRegistrationInformation.getName()!=null){
            queryWrapper.like(AsRegistrationInformation::getName, asRegistrationInformation.getName());
        }
        Page<AsRegistrationInformation> asRegistrationInformationPage = asRegistrationInformationMapper.selectPage(page, queryWrapper);
        return asRegistrationInformationPage;
    }



    /**
     * 根据名称选择社区注册信息
     * 如果提供的名称为空或空字符串，函数将返回一个空的列表
     * 否则，它将使用提供的名称作为条件查询数据库中的社区注册信息
     *
     * @param name 社区的名称
     * @return 与提供的名称匹配的社区注册信息列表
     */
    @Override
    public List<AsRegistrationInformation> selectAsRegistrationInformation(String name) {
        // 检查名称是否为空或空字符串，如果是，返回一个空的列表
        if (name == null || name.trim().isEmpty()) {
            return List.of();
        }

        // 创建一个查询包装器，并设置查询条件为名称包含提供的名称
        QueryWrapper<AsRegistrationInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);

        // 使用查询包装器从数据库中查询社区注册信息列表
        return asRegistrationInformationMapper.selectList(queryWrapper);
    }

}
